﻿using Core.Tools.IOC;
using Prism.Commands;
using Prism.Modularity;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using UI.Application.Share.Names;
using UI.Application.Share.Reactor;

namespace UI.Application.Main.ViewModels
{
    internal class MainViewModel : ReactiveObject
    {
        private IReactorManager ReactorManager { get; }
        public ICommand LoadedCommand { get; }
        public ICommand MinCommand { get; }
        public ICommand MaxCommand { get; }
        public ICommand CloseCommand { get; }
        public ICommand OpenIndexCommand { get; }
        public ICommand OpenConfigCommand { get; }
        public ICommand OpenReactorCommand { get; }

        public MainViewModel(IReactorManager reactorManager)
        {
            ReactorManager = reactorManager;

            LoadedCommand = new DelegateCommand(Loaded);
            MinCommand=new DelegateCommand(MinWindow);
            MaxCommand = new DelegateCommand(MaxWindow);
            CloseCommand = new DelegateCommand(CloseWindow);
            OpenIndexCommand = new DelegateCommand(OpenIndex);
            OpenConfigCommand = new DelegateCommand(OpenConfig);
            OpenReactorCommand = new DelegateCommand<object>(OpenReactor);
        }

        private void OpenReactor(object param)
        {
            PrismProvider.RegionManager.RequestNavigate(RegionNames.ContainerRegion, ViewNames.ReactorView);
            ReactorManager.SelectReactor(param);
        }

        /// <summary>
        /// 打开首页
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void OpenIndex()
        {
            PrismProvider.RegionManager.RequestNavigate(RegionNames.ContainerRegion, ViewNames.IndexView);
        }

        private void OpenConfig()
        {
            PrismProvider.RegionManager.RequestNavigate(RegionNames.ContainerRegion, ViewNames.ConfigView);
        }

        private void CloseWindow()
        {
            System.Windows.Application.Current?.MainWindow?.Close();
        }

        Rect rect;//定义一个全局Rect记录还原状态下窗口的位置和大小
        WindowState windowState = System.Windows.WindowState.Normal;
        private void MaxWindow()
        {
            var window = System.Windows.Application.Current.MainWindow;
            if (windowState == WindowState.Normal)
            {
                rect = new Rect(window.Left, window.Top, window.Width, window.Height);
                window.Left = 0;
                window.Top = 0;
                Rect rc = SystemParameters.WorkArea;
                window.Width = rc.Width;
                window.Height = rc.Height;
                windowState = WindowState.Maximized;
            }
            else
            {
                window.Left = rect.Left;
                window.Top = rect.Top;
                window.Width = rect.Width;
                window.Height = rect.Height;
                windowState = WindowState.Normal;
            }
        }

        private void MinWindow()
        {
            System.Windows.Application.Current.MainWindow.WindowState = System.Windows.WindowState.Minimized;
        }

        private void Loaded()
        {
            PrismProvider.ModuleManager.LoadModule(ModuleNames.ApplicationIndexModule);
            PrismProvider.RegionManager.RequestNavigate(RegionNames.ContainerRegion, ViewNames.IndexView);
            PrismProvider.MainWindow.WindowState = WindowState.Maximized;
        }
    }
}
